#include "game.h"
#include "player.h"
#include "ai.h"
#include <iostream>

Game::Game() : gameOver(false) {}

void Game::initialize() {
    board.initialize();
    
    // 创建玩家：人类 vs AI
    player1 = std::make_unique<HumanPlayer>("玩家1", Piece::BLACK);
    player2 = std::make_unique<AIPlayer>("电脑", Piece::WHITE);
    
    std::cout << "=== 五子棋游戏开始 ===\n";
    std::cout << "玩家1: X(黑子)\n";
    std::cout << "电脑: O(白子)\n";
}

void Game::run() {
    Player* currentPlayer = player1.get();
    
    while (!gameOver) {
        board.display();
        
        currentPlayer->makeMove(board);
        
        int lastX = 0, lastY = 0; // 这里应该记录最后落子位置
        if (board.checkWin(lastX, lastY)) {
            std::cout << currentPlayer->getName() << " 获胜！\n";
            gameOver = true;
        } else if (board.isFull()) {
            std::cout << "平局！\n";
            gameOver = true;
        }
        
        // 切换玩家
        board.switchPlayer();
        currentPlayer = (currentPlayer == player1.get()) ? player2.get() : player1.get();
    }
    
    board.display();
}

void Game::displayResult() const {
    std::cout << "游戏结束！\n";
}